package com.jmu.aslongastoday.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jmu.aslongastoday.common.Result;
import com.jmu.aslongastoday.domain.Collect;
import com.jmu.aslongastoday.dto.CollectInputDto;
import com.jmu.aslongastoday.service.CollectService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("collect")
public class CollectController {
    @Resource
    CollectService collectService;


    @PostMapping("add")
    public Result add(@RequestBody CollectInputDto dto){
        QueryWrapper<Collect> wrapper = new QueryWrapper<Collect>().eq("user_name", dto.getUserName()).
                eq("collect_item_name", dto.getCollectItemName());
        Collect one = collectService.getOne(wrapper);
        if(one!=null){
            return Result.fail("重复项");
        }


        Collect collect = new Collect();
        collect.setCollectItemAddr(dto.getCollectItemAddr());
        collect.setCollectItemName(dto.getCollectItemName());
        collect.setCreateTime(new Date());
        collect.setUserName(dto.getUserName());
        collectService.save(collect);
        return Result.success("");
    }

    @GetMapping("get")
    public Result getAll(@RequestParam String userName){
        QueryWrapper<Collect> collectQueryWrapper = new QueryWrapper<>();
        collectQueryWrapper.eq(userName!=null&&!userName.isEmpty(),"user_name",userName);
        collectQueryWrapper.orderByDesc("collect_id");
        List<Collect> list = collectService.list(collectQueryWrapper);

        return Result.success(list);
    }
}
